﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DreamCMS.WebCore.Common
{
    public class IOHelper
    {
        /// <summary>
        /// 递归文件数量
        /// </summary>
        /// <param name="dirp">物理路径</param>
        /// <returns>文件夹内的文件数量</returns>
        public static long GetDirectoryCount(string dirp)
        {
            long UpfileCount = 0;
            try
            {
                DirectoryInfo mydir = new DirectoryInfo(dirp);
                foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
                {
                    if (fsi is FileInfo)
                    {
                        //   FileInfo fi = (FileInfo)fsi;
                        UpfileCount += 1;
                    }
                    else
                    {
                        DirectoryInfo di = (DirectoryInfo)fsi;
                        string new_dir = di.FullName;
                        GetDirectoryCount(new_dir);
                    }
                }
            }
            catch
            {

            }

            return UpfileCount;
        }



        /// <summary>
        /// 递归文件夹大小（字节数）
        /// </summary>
        /// <param name="dirp">目录的物理路径</param>
        /// <returns>文件夹的大小</returns>
        public static long GetDirectorySize(string dirp)
        {
            long dirSize = 0;
            try
            {

                DirectoryInfo mydir = new DirectoryInfo(dirp);
                foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
                {
                    if (fsi is FileInfo)
                    {
                        FileInfo fi = (FileInfo)fsi;
                        dirSize += fi.Length;
                    }
                    else
                    {
                        DirectoryInfo di = (DirectoryInfo)fsi;
                        string new_dir = di.FullName;
                        GetDirectorySize(new_dir);
                    }
                }
            }
            catch
            {

            }

            return dirSize;
        }

        /// <summary>
        /// 获取文件大小（字节数）
        /// </summary>
        /// <returns></returns>
        public static long GetFileLength(string filePath)
        {
            System.IO.FileInfo finfo = null;
            try
            {
                finfo = new FileInfo(filePath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return finfo.Length;
        }

        /// <summary>
        /// 获取一个long型字节容量的合理表示法。比如M或者KB
        /// </summary>
        /// <param name="size">byte</param>
        /// <returns></returns>
        public static string GetFileSize(long size)
        {
            string FileSize = string.Empty;
            if (size > (1024 * 1024 * 1024))
                FileSize = ((double)size / (1024 * 1024 * 1024)).ToString(".##") + " GB";
            else if (size > (1024 * 1024))
                FileSize = ((double)size / (1024 * 1024)).ToString(".##") + " MB";
            else if (size > 1024)
                FileSize = ((double)size / 1024).ToString(".##") + " KB";
            else if (size == 0)
                FileSize = "0 Byte";
            else
                FileSize = ((double)size / 1).ToString(".##") + " Byte";
            return FileSize;
        }
        /// <summary>
        /// 获取文件扩展名
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetExtName(string str)
        {
            string ext = null;
            if (str.IndexOf('.') > 0)
            {
                string[] fs = str.Split('.');
                ext = fs[fs.Length - 1];
            }
            return "."+ext; 
        }
    }
}